<?php

/*
	Class: ModuleController.php

	About: Author
		J William McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
		<App_Controller_Action_Abstract>
*/
class ModuleController extends  App_Controller_Action_Abstract {

	/* Group: Instance Methods */

	/*
		Function: init
			Invoked automatically when an instance is created.
			For this class, does nothing other than initialize the parent object (calls init() on the parent instance).
	*/
	function init(){
        parent::init();
    }

	/* Group: Actions */

	/*
		Function: index
			The module admin landing page.
	*/
	
    function indexAction(){
    	$modules_table = new Modules();
    	$request = new App_Request($this->getRequest());

    	if($request->has("id") and $request->has("perform")){
    			
	    		switch($request->perform){
	    			case "enable":
		    			if(!$modules_table->isEnabled($request->id)){
		    				  if($modules_table->enable($request->id)){
		    				  	if(!is_null($modules_table->success)){
		    				  		$this->view->success = $modules_table->success;
		    				  	} else {
		    				  		$this->view->success = "Module \"".$request->id ."\" enabled.";
		    				  	}	
		    				  }
		    			} else {
		    				$this->view->notice = "Module \"".$request->id ."\" is already enabled.";
		    			}
	    			break;
	    			
	    			case "disable":
	    				if($modules_table->isEnabled($request->id)){
		    				  if($modules_table->disable($request->id)){
		    				  	if(!is_null($modules_table->success)){
		    				  		$this->view->success = $modules_table->success;
		    				  	} else {
		    				  		$this->view->success = "Module \"".$request->id ."\" disabled.";
		    				  	}	
		    				  }  				  
		    			} else {
		    				$this->view->notice = "Module \"".$request->id ."\" is already disabled.";
		    			}
	    			break;
	    			
	    			case "install":
	    				
	    				if(!$modules_table->exists($request->id)){
	    					if($modules_table->install($request->id)){
		    				  	if(!is_null($modules_table->success)){
		    				  		$this->view->success = $modules_table->success;
		    				  	} else {
		    				  		$this->view->success = "Module \"".$request->id ."\" installed.";
		    				  	}	
		    				  }  
	    				} else {
	    					$this->view->notice = "Module \"".$request->id ."\" is already installed.";
	    				}	    			
	    			break;
	    			
	    			case "uninstall":
	    				if($modules_table->exists($request->id)){
		    				 if($modules_table->disable($request->id)){
		    				 	if($modules_table->uninstall($request->id)){
			    				  	if(!is_null($modules_table->success)){			    				  						  		
			    				  		$this->view->success = $modules_table->success;
			    				  	} else {
			    				  		$this->view->success = "Module \"".$request->id ."\" disabled and uninstalled.";
			    				  	}	
		    				  	} 
		    				 }
	    				} else {
	    					$this->view->notice = "Module \"".$request->id ."\" is not installed.";
	    				}
	    			break;	
	    			
	    		
    		}
    		if(count($modules_table->errors) > 0){
    			$this->view->errors = $modules_table->errors;
    		}
    		
    		if(!is_null($modules_table->notice)){
    			$this->view->notice = $modules_table->notice;
    		}    		

    	}
    	
    	$basepath = Zend_Registry::get('basepath');
		$module_dir = $basepath."/modules";
		$o_module_dir = dir($module_dir );
		$available_modules = array();
		
		while (false !== ($entry = $o_module_dir->read())) {
			if (substr($entry, 0, 1) != "."){
				if($entry != "default"){
					$full_dir = $module_dir . "/" . $entry;
					if(file_exists($full_dir . "/module.ini") and !$modules_table->exists($entry)){
						$tmp_module = $modules_table->parseIni($entry);
						$tmp_module['id'] = $entry;	
		   				$available_modules[] = $tmp_module;
		   			}					
				}		
			}
		}
		$o_module_dir->close(); 
    	
    	$this->view->available_modules = $available_modules;
    	
		$tmp_modules = array();
    	$modules = $modules_table->fetchAll(null,"id");
    	$total_enabled = 0;
    	$total_disabled = 0;
    	if(count($modules) > 0){
    		$tmp_modules = array();
    		foreach($modules as $module){
    			$module = $module->toArray();
    			$config = $modules_table->parseIni($module['id']);
    			
    			foreach($config as $key => $val){
    				$module[$key] = $val;
    			}
    			if($module['is_enabled'] == 1){
					$total_enabled++;
				}else{
					$total_disabled++;
				}
    			$tmp_modules[] = $module;
    		}

			
    		$this->view->modules = $tmp_modules;
    		$this->view->total_enabled = $total_enabled;
    		$this->view->total_disabled = $total_disabled;
    	}
    	
    	
    	
    }

    
}